package com.rbcs.borrow.servive;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rbcs.borrow.model.Borrow;
import com.rbcs.flow.FlowConstants;
import com.rbcs.flow.service.FlowManager;
import com.systop.core.ApplicationException;
import com.systop.core.service.BaseGenericsManager;

/**
 * 借款申请管理Manager
 * @author ShangHua
 */
@Service
public class BorrowManager extends BaseGenericsManager<Borrow> {
	
	//流程管理Manager
	@Autowired
	private FlowManager flowManager;
	
	/**
	 * 保存借款申请信息
	 */
	@Transactional
	public void save(Borrow borrow) {
		if (getDao().exists(borrow, "singleNo")) {
			if (StringUtils.isNotEmpty(borrow.getSingleNo())) {
				throw new ApplicationException("借款单号为【" + borrow.getSingleNo() + "】的信息已存在!");
			}
		}
		// 保存借款申请表
		super.save(borrow);
	}

	/**
	 * 保存流程表
	 * @param borrow
	 */
	@Transactional
	public void saveFlows(Borrow borrow) {
		if (borrow != null) {
			//设置借款单的审批状态为"待审批"
			borrow.setAudStatus(FlowConstants.AUDIT_PENDING);
			getDao().saveOrUpdate(borrow);
			//创建借款流程
			flowManager.createBorrowFlow(borrow);
		}
	}

}
